#ifndef PERIPHERALS_H
#define	PERIPHERALS_H

#ifdef	__cplusplus
extern "C" {
#endif

#define KVA_TO_PA(a)        ((int)a < 0 ? ((int)a&0x1fffffffL) : (unsigned int)((unsigned char*)a+0x40000000L))

#define SPI_SD				SPI_CHANNEL1
#define SPI_SD_TX_IRQ		_SPI1_TX_IRQ
#define SPI_SD_RX_IRQ		_SPI1_RX_IRQ
#define SPI_SD_CONbits          SPI1CONbits
#define SPI_SD_STATbits          SPI1STATbits
    
#define DMA_SD_TX			DMA_CHANNEL5
#define DMA_SD_TX_VECTOR	_DMA_5_VECTOR
#define DMA_SD_RX			DMA_CHANNEL0
#define DMA_SD_RX_VECTOR	_DMA_0_VECTOR
	
#define UART_RC				UART4 // U1B
#define UART_RC_VECTOR   _UART_4_VECTOR
#define UART_RC_STAbits     U4STAbits

#define UART_BT				UART2 // U3A
#define DMA_BT_TX			DMA_CHANNEL2
#define DMA_BT_TX_ISR		_UART2_TX_IRQ
#define DMA_BT_TX_VECTOR	_DMA_2_VECTOR
	
#define DMA_BT_RX			DMA_CHANNEL3
#define DMA_BT_RX_ISR		_UART2_RX_IRQ
#define DMA_BT_RX_VECTOR	_DMA_3_VECTOR

#define UART_GPS			UART6 // U2B

#define PRIORITY_I2C		INT_PRIORITY_LEVEL_6
#define PRIORITY_SD_TX		INT_PRIORITY_LEVEL_4
#define PRIORITY_SD_RX		INT_PRIORITY_LEVEL_5
#define PRIORITY_BT			INT_PRIORITY_LEVEL_4
#define PRIORITY_RC			INT_PRIORITY_LEVEL_3

#define BUZZ_DELAY_ERROR    (configTICK_RATE_HZ / 2)
#define BUZZ_DELAY_NOTIFY   (configTICK_RATE_HZ / 12)






#define LED1_ON         PORTESET = 1 << 0
#define LED1_OFF        PORTECLR = 1 << 0
#define LED2_ON         PORTESET = 1 << 1
#define LED2_OFF        PORTECLR = 1 << 1
#define LED3_ON 		PORTGSET = 1 << 14
#define LED3_OFF        PORTGCLR = 1 << 14
#define LED4_ON         PORTGSET = 1 << 12
#define LED4_OFF        PORTGCLR = 1 << 12



#define LEDG_TOGGLE     PORTEINV = 1 << 3
#define LEDG_ON         PORTESET = 1 << 3
#define LEDG_OFF        PORTECLR = 1 << 3

#define LEDR_TOGGLE     PORTAINV = 1 << 0
#define LEDR_ON         PORTASET = 1 << 0
#define LEDR_OFF        PORTACLR = 1 << 0

#define LEDB_TOGGLE     PORTGINV = 1 << 13
#define LEDB_ON         PORTGSET = 1 << 13
#define LEDB_OFF        PORTGCLR = 1 << 13

#define LEDO_TOGGLE     PORTEINV = 1 << 2
#define LEDO_ON         PORTESET = 1 << 2
#define LEDO_OFF        PORTECLR = 1 << 2

#define LEDW_TOGGLE     PORTEINV = 1 << 4
#define LEDW_ON         PORTESET = 1 << 4
#define LEDW_OFF        PORTECLR = 1 << 4

#define BUZZ_ON 		PORTASET = 1 << 9
#define BUZZ_OFF        PORTACLR = 1 << 9
#define BUZZ_TOGGLE     PORTAINV = 1 << 9

#define BENCH_BUSY      PORTFSET = 1 << 8
#define BENCH_IDLE      PORTFCLR = 1 << 8

#define SD_CS_HIGH      PORTDSET = 1 << 9
#define SD_CS_LOW       PORTDCLR = 1 << 9
#define SD_DO_HIGH      { TRISDCLR = _TRISD_TRISD0_MASK; /*  PORTSetPinsDigitalOut(IOPORT_D,BIT_0);*/ PORTDSET = 1 << 0; }

#define BLUETOOTH_RESET_ON  PORTBCLR = 1 << 15
#define BLUETOOTH_RESET_OFF PORTBSET = 1 << 15

void BUZZ(int delay, int beeps);


#ifdef	__cplusplus
}
#endif

#endif	/* PERIPHERALS_H */

